package Phase;

import Entities.*;
import StateInformation.GameState;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class PhaseController implements KeyListener {

    private ArkanoidPanel arkanoidPanel;
    private String phase;
    private GameState gameState;
    private ArkanoidController arkanoidController;
    private Scene scene;
    private Bar bar;
    private Ball ball;
    private AnimationController animationController;
    private Renderer renderer;

    public PhaseController(String phase, ArkanoidPanel arkanoidPanel, GameState gameState, ArkanoidController arkanoidController) {
        this.arkanoidPanel = arkanoidPanel;
        this.arkanoidController = arkanoidController;
        this.phase = phase;
        this.gameState = gameState;

        bar = new Bar(192, 250);
        ball = new Ball(200, 232, 0, 0, 8);
    }

    void setPhase(String phase) {
        this.phase = phase;
    }

    public void execute() {
        if (!gameState.getFinalLevel().equals("")) {
            for (int i = 0; i < gameState.getScene().getNumElems(); i++) {
                if (gameState.getScene().getChild(i) instanceof Ball) {
                    Ball b = (Ball) gameState.getScene().getChild(i);
                    gameState.getScene().remove(b);
                } else if (gameState.getScene().getChild(i) instanceof Bar){
                    Bar b = (Bar) gameState.getScene().getChild(i);
                    gameState.getScene().remove(b);
                }
            }
            gameState.getScene().addChild(ball);
            gameState.getScene().addChild(bar);
            
            renderer = new Renderer(arkanoidPanel, gameState.getScene(), gameState);
            animationController = new AnimationController(gameState.getScene(), gameState, bar, ball, 400, this);

            renderer.start();
            animationController.start();
        } else {
            scene = new FilePhaseLoader(phase).load();
            scene.addChild(ball);
            scene.addChild(bar);

            renderer = new Renderer(arkanoidPanel, scene, gameState);
            animationController = new AnimationController(scene, gameState, bar, ball, 400, this);
            
            gameState.setScene(scene);
            
            renderer.start();
            animationController.start();
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            animationController.setRigthPressed(true);
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            animationController.setLeftPressed(true);
        } else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            animationController.setSpacePressed(true);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            animationController.setRigthPressed(false);
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            animationController.setLeftPressed(false);
        } else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            animationController.setSpacePressed(false);
        }
    }

    public void finish() {
        renderer.setTerminar(true);
        animationController.setTerminar(true);
        arkanoidController.phaseFinished(this);
    }

    public void gameOver() {
        renderer.setTerminar(true);
        animationController.setTerminar(true);
        arkanoidController.gameOver();
    }

    public void ballLost() {
        arkanoidController.ballLost();
    }
}
